﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Itasa.API
{
    public class ITASA_user
    {

        internal long ID { get; private set; }
        internal String Name { get; private set; }
        internal String Email { get; private set; }
        internal DateTime LastVisit { get; private set; }
        internal String AuthCode { get; private set; }
        internal bool HasMyItasa { get; private set; }
        internal String UserName { get; private set; }
        internal DateTime RegisterDate { get; private set; }
        internal String Gender { get; private set; }
        internal DateTime BirthDate { get; private set; }
        internal String Location { get; private set; }
        internal String WebSiteUrl { get; private set; }
        internal int UnreadMessages { get; private set; }
        internal String Avatar { get; private set; }
        internal int KarmaGood { get; private set; }
        internal int KarmaBad { get; private set; }

        private ITASA_user(XmlNode user)
        {
            Name = Email = AuthCode = UserName = Gender = Location = WebSiteUrl = Avatar = null;
            LastVisit = RegisterDate = BirthDate = DateTime.MinValue;
            HasMyItasa = false;
            ID = KarmaBad = KarmaGood = UnreadMessages = -1;
            foreach (XmlNode ud in user.ChildNodes)
            {
                if (ud.InnerText != null)
                {
                    if (ud.Name == "id") ID = long.Parse(ud.InnerText);
                    if (ud.Name == "name") Name = ud.InnerText;
                    if (ud.Name == "email") Email = ud.InnerText;
                    if (ud.Name == "last_visit") LastVisit = DateTime.Parse(ud.InnerText);
                    if (ud.Name == "authcode") AuthCode = ud.InnerText;
                    if (ud.Name == "has_myitasa") HasMyItasa = (int.Parse(ud.InnerText)==0 ? false : true);
                    if (ud.Name == "username") UserName = ud.InnerText;
                    if (ud.Name == "register_date") RegisterDate = DateTime.Parse(ud.InnerText);
                    if (ud.Name == "gender") Gender = ud.InnerText;
                    if (ud.Name == "birthdate") BirthDate = DateTime.Parse(ud.InnerText);
                    if (ud.Name == "location") Location = ud.InnerText;
                    if (ud.Name == "website_url") WebSiteUrl = ud.InnerText;
                    if (ud.Name == "unread_messages") UnreadMessages = Int32.Parse(ud.InnerText);
                    if (ud.Name == "avatar") Avatar = ud.InnerText;
                    if (ud.Name == "karma_bad") KarmaBad = Int32.Parse(ud.InnerText);
                    if (ud.Name == "karma_good") KarmaGood = Int32.Parse(ud.InnerText);
                }
            }
        }

        public static ITASA_user getInstance(XmlDocument xd)
        {
            XmlNode root = xd.DocumentElement;
            if (root.Name == "root")
            {
                if (!root.ChildNodes[0].HasChildNodes && root.ChildNodes[1].InnerText == "fail")
                {
                    XmlNode error = root.ChildNodes[2];
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("Error Code {0}: {1}", error.FirstChild.InnerText, error.LastChild.InnerText);
                    Params.writeLog(sb.ToString());
                    return null;
                }
                else
                {
                    ITASA_user mU = new ITASA_user(root.ChildNodes[0].FirstChild);
                    return mU;
                }
            }
            return null;
        }
        
    }
}
